﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AAA.Base.Util
{
    public class StringHelper
    {
	    private const string NUMBER_CHAR = "0123456789";
        private const string SIMPLE_CHINESE_NUMBER = "零一二三四五六七八九";
        private const string CHINESE_NUMBER = "零壹貳叁肆伍陸柒捌玖";
        private const string FULL_NUMBER_CHAR = "０１２３４５６７８９";

        public static string ToString(object oValue)
        {
            return ToString(oValue, ",");
        }
        public static string ToString(object oValue, string strSplitDelimeter)
        {
            if (Convert.IsDBNull(oValue))
                return "null";

            if (oValue == null)
                return "null";

            return (oValue is DateTime)
                    ? ((DateTime)oValue).Ticks == DateTime.MinValue.Ticks
                        ?   "null"
                        :   ((DateTime)oValue).ToString("yyyy/MM/dd HH:mm:ss")
                    : oValue.GetType().IsArray
                        ?   ArrayToString((object[])oValue, strSplitDelimeter)
                        :   oValue.ToString();
        }

        public static string ToTitleCase(string strSource)
        {
            if (string.IsNullOrEmpty(strSource))
                return strSource;

            return strSource.Substring(0, 1).ToUpper() +
                    (strSource.Length > 1 ? strSource.Substring(1).ToLower() : "");
        }

        public static string HalfToFullNumberChar(string strSource)
        {
            int iIndex = -1;
            string strReturn = "";

            for (int i = 0; i < strSource.Length; i++)
            {
                iIndex = NUMBER_CHAR.IndexOf(strSource.Substring(i, 1));
                if (iIndex < 0)
                {
                    strReturn += strSource.Substring(i, 1);
                }
                else
                {
                    strReturn += FULL_NUMBER_CHAR.Substring(iIndex, 1);
                }
            }
            return strReturn;
        }

        public static string TrimLast(string strSource, string strTrimString)
        {
            if (strSource == null)
                return strSource;

            while (strSource.EndsWith(strTrimString))
                strSource = strSource.Substring(0, strSource.Length - strTrimString.Length);
            return strSource;
        }

        private static string ReplaceByChar(string strSource, string strFrom, string strTo)
        {
            for (int i = 0; i < Math.Min(strFrom.Length, strTo.Length); i++)
                strSource = strSource.Replace(strFrom.Substring(i, 1),
                                              strTo.Substring(i, 1));
            return strSource;
        }

        public static string SimpleChineseFromFullNumberString(string strSource)
        {
            return ReplaceByChar(strSource, FULL_NUMBER_CHAR, SIMPLE_CHINESE_NUMBER);
        }

        public static string NumberStringFromSimpleChinese(string strSource)
        {
            return ReplaceByChar(strSource, SIMPLE_CHINESE_NUMBER, NUMBER_CHAR);
        }

        public static string NumberStringFromChinese(string strSource)
        {
            return ReplaceByChar(strSource, CHINESE_NUMBER, NUMBER_CHAR);
        }

        public static string ChineseFromNumberString(string strSource)
        {
            return ReplaceByChar(strSource, NUMBER_CHAR, CHINESE_NUMBER);
        }

        public static string SimpleChineseFromNumberString(string strSource)
        {
            return ReplaceByChar(strSource, NUMBER_CHAR, SIMPLE_CHINESE_NUMBER);
        }
	
        public static string Utf8ToBig5(string strUtf)
        {
            return CharSetConvert(strUtf, Encoding.GetEncoding("utf-8"), Encoding.GetEncoding("big5"));
        }

        public static string CharSetConvert(string strSource, Encoding eSource, Encoding eTarget)
        {
            return eTarget.GetString(Encoding.Convert(eSource, eTarget, eSource.GetBytes(strSource)));
/*
            byte[] bSource = eSource.GetBytes(strSource.Trim());
            byte[] bTarget = Encoding.Convert(eSource, eTarget, bSource);

            char[] cTarget = new char[eTarget.GetCharCount(bTarget, 0, bTarget.Length)];

            eTarget.GetChars(bTarget, 0, bTarget.Length, cTarget, 0);

            return new string(cTarget);
 */ 
        }

        public static Encoding GetEncoding(string strEncoding)
        {            
            switch (strEncoding.ToLower())
            {
                case "big5":
                    return Encoding.Default;

                case "utf8":
                    return Encoding.UTF8;

                case "utf7":
                    return Encoding.UTF7;

                case "utf32":
                    return Encoding.UTF32;

                case "ascii":
                    return Encoding.ASCII;

                case "unicode":
                    return Encoding.Unicode;

                case "bigendianunicode":
                    return Encoding.BigEndianUnicode;
            }
            return Encoding.Default;
        }

        /// <summary>
        /// 將字串結尾補空白字元
        /// </summary>
        /// <param name="strData">欲處理的字串</param>
        /// <param name="intLen">欲達到的Bytes</param>
        /// <returns></returns>
        public static string FillSpace(string strData, int intLen)
        {
            Encoding enc = Encoding.GetEncoding("Big5");//提供big5的編解碼
            int intDataLen = enc.GetByteCount(strData);
            if (intDataLen < intLen)
            {
                StringBuilder strRtnData = new StringBuilder(strData);
                strRtnData.Append(' ', intLen - strData.Length);
                return strRtnData.ToString();
            }
            else
            {
                return strData;
            }
        }

        /// <summary>
        /// 過濾結束字元\0
        /// </summary>
        /// <param name="strFilterData">欲過濾的字串資料</param>
        /// <returns></returns>
        public static string FilterBreakChar(string strFilterData)
        {
            Encoding enc = Encoding.GetEncoding("Big5");//提供Big5的編解碼
            byte[] tmp_bytearyData = enc.GetBytes(strFilterData);
            int intCharLen = tmp_bytearyData.Length;
            int indexCharData = intCharLen;
            for (int i = 0; i < intCharLen; i++)
            {
                if (Convert.ToChar(tmp_bytearyData.GetValue(i)) == 0)
                {
                    indexCharData = i;
                    break;
                }
            }
            return enc.GetString(tmp_bytearyData, 0, indexCharData);
        }

        public static string Substring(string strSource, int iStart, int iLength)
        {
            if (iLength <= 0)
                return "";

            Encoding eEncoding = Encoding.GetEncoding("big5", new EncoderExceptionFallback(), new DecoderReplacementFallback(""));
            byte[] bSource = eEncoding.GetBytes(strSource);

            if (iStart + 1 > bSource.Length)
                return "";

            if (iStart + iLength > bSource.Length)
                iLength = bSource.Length - iStart;

            return eEncoding.GetString(bSource, iStart, iLength);
        }

        public static string[] Split(string strSource, int[] iLengths)
        {
            string[] strReturn = new string[iLengths.Length];
            int iStart = 0;

            for (int i = 0; i < iLengths.Length; i++)
            {
                strReturn[i] = Substring(strSource, iStart, iLengths[i]);
                iStart += iLengths[i];
            }
            return strReturn;
        }

        public static string FillChar(string strSource, char cFill, int iLength, StringFillTypeEnum eFillType)
        {
            if (strSource.Length >= iLength)
                return strSource;

            int iLeftPadLength;
            int iRightPadLength;

            switch (eFillType)
            {
                case StringFillTypeEnum.Left:
                    strSource = strSource.PadLeft(iLength, cFill);
                    break;
                case StringFillTypeEnum.Center:
                    iLeftPadLength = (int)Math.Floor((iLength - strSource.Length) / 2.0);
                    iRightPadLength = iLength - strSource.Length - iLeftPadLength;
                    strSource.PadLeft(iLeftPadLength, cFill).PadRight(iRightPadLength, cFill);
                    break;
                case StringFillTypeEnum.Right:
                    strSource = strSource.PadRight(iLength, cFill);
                    break;

            }

            return strSource;
        }

        public static string TrimToSingleSpace(string strSource)
        {
            while (strSource.IndexOf("  ") > -1)
                strSource = strSource.Replace("  ", " ");
            return strSource;
        }

        public static string CapitalizeFirstWord(string strSource)
        {
            return strSource.ToUpper()[0] + strSource.ToLower().Substring(1);
        }

        public static string ArrayToString(object[] oSource, string strDelimeter)
        {
            string strReturn = "";

            for (int i = 0; i < oSource.Length; i++)
                strReturn += strDelimeter + oSource[i].ToString();

            return strReturn.Length > strDelimeter.Length 
                    ?   strReturn.Substring(strDelimeter.Length)
                    :   strReturn ;

        }
    }
}
